"""
threading.current_thread
        Return the current Thread object, corresponding to the caller's thread of control.

    返回当前线程对象:
    >>> threading.current_thread
    <function current_thread at 0x00000000029F6C80>
    >>> threading.current_thread()
    <_MainThread(MainThread, started 4912)>
    >>> type(threading.current_thread())
    <class 'threading._MainThread'>

    继承线程threading方法；通过help(threading.current_thread())查看。

"""
""""
import threading,time

lock = threading.Lock() # 通过锁来实现线程执行顺序

def run(n):
    lock.acquire()
    try:
        thread = threading.current_thread()
        thread.setName('Thread-***%s***' % n)
        print('-'*30)
        print("\n Pid is :%s" % thread.ident) ##返回PID
        print("\n ThreadName is :%s" % thread.getName())##返回线程名称
    finally:
        lock.release()
if __name__ == "__main__":
    
    for i in range(3):
        thread_alive = threading.Thread(target=run,args=(i,))
        thread_alive.start()
    thread_alive.join()
    print('\n%s thread is done...'% threading.current_thread().getName())


"""

import threading,time

def run(n):
    semaphore.acquire() 
    time.sleep(1)
    thread = threading.current_thread()
    thread.setName('Thread-***%s***'% n)
    print('-'*30)
    print("\n Pid is :%s" % thread.ident) ##返回PID
    print("\n ThreadName is :%s" % thread.getName())##返回线程名称

    # 如果再次释放信号量，信号量加一，这是超过限定的信号量数目，这时会报错ValueError: Semaphore released too many times
    semaphore.release()

if __name__ == "__main__":
    num = 0
    semaphore = threading.BoundedSemaphore(2) #最多允许两个线程同时运行
    for i in range(3):
        thread_alive = threading.Thread(target=run,args=(i,))
        thread_alive.start()
    thread_alive.join()
    print('\n%s thread is done...'% threading.current_thread().getName())

# while threading.active_count() != 1:
#     pass
# else:
#     print('----all threads done---')
#     print(num)
